﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerKhauPhanAnDao : IKhauPhanAnDao
    {
        public SqlServerKhauPhanAnDao()
        {
            SQLUtility.TableName = "KhauPhanAn";
        }

        //Trả về đối tượng báo ăn
        private KhauPhanAn MakeKhauPhanAn(DataRow row)
        {
            Guid _id=Guid.Parse(row["Id"].ToString());
            DateTime _ngayLap = DateTime.Parse(row["NgayLap"].ToString());
            int _soLuongTre = int.Parse(row["SoLuongTre"].ToString());
            int _schoolID = int.Parse(row["SchoolID"].ToString());
            Guid _blockiId = Guid.Parse(row["BlockiId"].ToString());

         return new KhauPhanAn(_id, _ngayLap, _soLuongTre, _schoolID, _blockiId);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<KhauPhanAn> MakeKhauPhanAns(DataTable dt)  
        {
            IList<KhauPhanAn> list = new List<KhauPhanAn>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeKhauPhanAn(row));
            }
            return list;
        }
        private DbParameter[] Take(KhauPhanAn KhauPhanAn)
        {
            DbParameter[] parameters = new DbParameter[5];
            parameters[0] = new SqlParameter("@Id", KhauPhanAn.Id);
            parameters[1] = new SqlParameter("@NgayLap", KhauPhanAn.NgayLap);
            parameters[2] = new SqlParameter("@SoLuongTre", KhauPhanAn.SoLuongTre);
            parameters[3] = new SqlParameter("@SchoolID", KhauPhanAn.SchoolID);
            parameters[4] = new SqlParameter("@BlockiId", KhauPhanAn.BlockiId);
            return parameters;
        }
        public IList<Business_Objects.KhauPhanAn> GetKhauPhanAn()
        {
            SQLUtility.TableName = "KhauPhanAn";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeKhauPhanAns(dt); 
        }

        public Business_Objects.KhauPhanAn GetKhauPhanAnByID(string KhauPhanAnID)
        {
            SQLUtility.TableName = "KhauPhanAn";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@Id", KhauPhanAnID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeKhauPhanAn(row);
            }
        }

        public void InsertKhauPhanAn(Business_Objects.KhauPhanAn KhauPhanAn)
        {
            SQLUtility.TableName = "KhauPhanAn";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(KhauPhanAn), true);
        }

        public int UpdateKhauPhanAn(Business_Objects.KhauPhanAn KhauPhanAn)
        {
            SQLUtility.TableName = "KhauPhanAn";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(KhauPhanAn)); }
            catch { return -1; }
        }

        public int DeleteKhauPhanAn(Business_Objects.KhauPhanAn KhauPhanAn)
        {
            SQLUtility.TableName = "KhauPhanAn";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(KhauPhanAn)); }
            catch { return -1; }
        }
    }
}
